package cn.kgcnews.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.kgcnews.pojo.NewsComment;
import cn.kgcnews.service.CommentService;
import cn.kgcnews.service.impl.CommentServiceImpl;

public class CommentServlet extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	this.doPost(request, response);
	
	}
	public void saveComment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String author = "作者没写";
		
		int nid=Integer.valueOf(request.getParameter("newsid"));
		
		
		NewsComment com=new NewsComment();
		com.setAuthorName(author);
		com.setnId(Integer.valueOf(nid));
		com.setCip("10.100.6");
		String comment=request.getParameter("con");
		com.setContext(comment);
		System.out.println(com.getAuthorName()+com.getCip()+com.getContext()+com.getnId());
		CommentServiceImpl csi = new CommentServiceImpl();
		HttpSession session=request.getSession();
		session.setAttribute("comments", com);
		boolean flag = csi.saveComment(com);
		if(flag){
			out.print("1");
		}else{
			out.println("0");
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
	
		
		
		
			
				this.saveComment(request, response);
			
			
		
		
		out.flush();
		out.close();
	}

	
	public void init() throws ServletException {
		
	}

}
